package cn.itsource.xhy.basic.controller;

import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.FastdfsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/fastDfs")
public class FastDFSController {

    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file){
        System.out.println(file);
        // 文件原始名
        String filename = file.getOriginalFilename();
        // 获取文件后缀，截取
        String substring = filename.substring(filename.lastIndexOf(".") + 1);
        try {
            // 通过工具类拿到文件路径
            String path = FastdfsUtil.upload(file.getBytes(), substring);
            // 路径返回给前端
            return AjaxResult.me().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
